package cn.kgc.crowdfundingadmin3.service;

import cn.kgc.crowdfundingadmin3.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadmin3.mapper.AdminMapper;
import cn.kgc.crowdfundingadmin3.pojo.Admin;
import cn.kgc.crowdfundingadmin3.pojo.AdminExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.List;

@Scope
@Service("adminService")
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public void updateAdmin(Admin admin) {
        // 对密码进行加密
        if (CrowdFundingUtils.stringEffective(admin.getUserpswd())){
            String userPswd = admin.getUserpswd();
            userPswd = CrowdFundingUtils.md5(userPswd);
            admin.setUserpswd(userPswd);
        }
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public Admin getAdminById(Integer adminId) {
        return adminMapper.selectByPrimaryKey(adminId);
    }

    @Override
    public void saveAdmin(Admin admin) {
        String userPswd = admin.getUserpswd();
        userPswd = CrowdFundingUtils.md5(userPswd);
        admin.setUserpswd(userPswd);
        // 执行保存
        adminMapper.insert(admin);
    }

    @Override
    public void batchRemove(List<Integer> adminIdList) {
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(adminIdList);
        adminMapper.deleteByExample(example);
    }

    @Override
    public PageInfo<Admin> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Admin> pageInfo=null;
        if (keyword!=null&&keyword.length()>0){
            AdminExample example=new AdminExample();
            AdminExample.Criteria criteria = example.createCriteria();
            criteria.andLoginacctLike("%"+keyword+"%");

            AdminExample.Criteria criteria2=example.createCriteria();
            criteria2.andUsernameLike("%"+keyword+"%");
            example.or(criteria2);

            AdminExample.Criteria criteria3=example.createCriteria();
            criteria3.andEmailLike("%"+keyword+"%");
            example.or(criteria3);
            List<Admin> list=adminMapper.selectByExample(example);
            pageInfo=new PageInfo<>(list);
        }else{
            List<Admin> list=adminMapper.selectByExample(null);
            pageInfo=new PageInfo<>(list);
        }
        return pageInfo;
    }

    @Override
    public Admin login(String loginAcct, String userPswd) {
        Admin admin=null;
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria=example.createCriteria();
        criteria.andLoginacctEqualTo(loginAcct);
        List<Admin> list=adminMapper.selectByExample(example);
        if (CrowdFundingUtils.collectionEffective(list)) {
            admin=list.get(0);
            String md5=CrowdFundingUtils.md5(userPswd);
            if(md5.equals(admin.getUserpswd())){
                return admin;
            }else{
                admin=null;
            }
        }
        return admin;
    }

    @Override
    public int countByExample(AdminExample example) {
        return 0;
    }

    @Override
    public int deleteByExample(AdminExample example) {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insert(Admin record) {
        return 0;
    }

    @Override
    public int insertSelective(Admin record) {
        return 0;
    }

    @Override
    public List<Admin> selectByExample(AdminExample example) {
        return null;
    }

    @Override
    public Admin selectByPrimaryKey(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(Admin record, AdminExample example) {
        return 0;
    }

    @Override
    public int updateByExample(Admin record, AdminExample example) {
        return 0;
    }

    @Override
    public int updateByPrimaryKeySelective(Admin record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Admin record) {
        return 0;
    }
}